﻿using System;
using System.Text;
using AventStack.ExtentReports;
using Ro.AssistFunction.ExtentReport;
using Ro.Common.RoInterface;

namespace Ro.AssistFunction.OutputManage
{
    /// <summary>
    /// 接口实现
    /// 输出至控制台
    /// <para>所有的OutputToConsole均不会产生Rolog输出</para>
    /// </summary>
    public class RoOutputToConsole : IRoOutputToConsole
    {
        /// <summary>
        /// 具有基础性质的console输出
        /// 不带有ER输出
        /// </summary>
        /// <param name="status">语句状态</param>
        /// <param name="outputstring">输出语句</param>
        public void OutputConsole(RoLogStatus status, string outputstring)
        {
            RoOutputPrivateFun roOutputPrivateFun = new RoOutputPrivateFun();
            string statusresult; //结果
            //通过方法反馈的额外信息
            string outstring;
            //逻辑判断
            switch (status)
            {
                case RoLogStatus.Pass:
                    Console.ForegroundColor = ConsoleColor.Green;
                    statusresult = "执行成功";
                    outstring = roOutputPrivateFun.RebackOutputConsole(outputstring, statusresult);
                    outstring = outstring.Replace("<br/>", "");
                    Console.WriteLine(outstring);
                    break;

                case RoLogStatus.Fail:
                    Console.ForegroundColor = ConsoleColor.Red;
                    statusresult = "执行遭遇失败";
                    outstring = roOutputPrivateFun.RebackOutputConsole(outputstring, statusresult);
                    outstring = outstring.Replace("<br/>", "");
                    Console.WriteLine(outstring);
                    break;

                case RoLogStatus.Fatal:
                    Console.ForegroundColor = ConsoleColor.Magenta;
                    statusresult = "执行遭遇致命错误";
                    outstring = roOutputPrivateFun.RebackOutputConsole(outputstring, statusresult);
                    outstring = outstring.Replace("<br/>", "");
                    Console.WriteLine(outstring);
                    break;

                case RoLogStatus.Warning:
                    statusresult = "执行警告";
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    outstring = roOutputPrivateFun.RebackOutputConsole(outputstring, statusresult);
                    outstring = outstring.Replace("<br/>", "");
                    Console.WriteLine(outstring);
                    break;

                case RoLogStatus.Info:
                    statusresult = "执行信息";
                    Console.ForegroundColor = ConsoleColor.Blue;
                    outstring = roOutputPrivateFun.RebackOutputConsole(outputstring, statusresult);
                    outstring = outstring.Replace("<br/>", "");
                    Console.WriteLine(outstring);
                    break;

                case RoLogStatus.Debug:
                    statusresult = "执行Debug信息";
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    outstring = roOutputPrivateFun.RebackOutputConsole(outputstring, statusresult);
                    outstring = outstring.Replace("<br/>", "");
                    Console.WriteLine(outstring);
                    break;
            }
        }


        /// <summary>
        /// 具有控件操作性质的console输出
        /// 带有ER输出
        /// </summary>
        /// <param name="status">语句状态</param>
        /// <param name="controlName">操作节点名称</param>
        /// <param name="controlId">控件Id</param>
        /// <param name="outputstring">输出语句</param>
        public void OutputConsole(RoLogStatus status, string controlName, string controlId, string outputstring)
        {
            //初始化
            RoOutputPrivateFun roOutputPrivateFun = new RoOutputPrivateFun();
            string statusresult;
            string outstring;
            //逻辑判断
            switch (status)
            {
                case RoLogStatus.Pass:
                    Console.ForegroundColor = ConsoleColor.Green;
                    statusresult = "执行成功";
                    outstring = roOutputPrivateFun.RebackOutputConsole(controlName, controlId, outputstring, statusresult);
                    roOutputPrivateFun.RebackExtentReport(Status.Pass, controlName, controlId, outstring);
                    outstring = outstring.Replace("<br/>", "");
                    Console.WriteLine(outstring);
                    break;

                case RoLogStatus.Fail:
                    Console.ForegroundColor = ConsoleColor.Red;
                    statusresult = "执行遭遇失败";
                    outstring = roOutputPrivateFun.RebackOutputConsole(controlName, controlId, outputstring, statusresult);
                    roOutputPrivateFun.RebackExtentReport(Status.Fail, controlName, controlId, outstring);
                    outstring = outstring.Replace("<br/>", "");
                    Console.WriteLine(outstring);
                    break;

                case RoLogStatus.Fatal:
                    Console.ForegroundColor = ConsoleColor.DarkRed;
                    statusresult = "执行遭遇致命错误";
                    outstring = roOutputPrivateFun.RebackOutputConsole(controlName, controlId, outputstring, statusresult);
                    roOutputPrivateFun.RebackExtentReport(Status.Fatal, controlName, controlId, outstring);
                    outstring = outstring.Replace("<br/>", "");
                    Console.WriteLine(outstring);
                    break;

                case RoLogStatus.Warning:
                    statusresult = "执行警告";
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    outstring = roOutputPrivateFun.RebackOutputConsole(controlName, controlId, outputstring, statusresult);
                    roOutputPrivateFun.RebackExtentReport(Status.Warning, controlName, controlId, outstring);
                    outstring = outstring.Replace("<br/>", "");
                    Console.WriteLine(outstring);
                    break;

                case RoLogStatus.Info:
                    statusresult = "执行信息";
                    Console.ForegroundColor = ConsoleColor.Blue;
                    outstring = roOutputPrivateFun.RebackOutputConsole(controlName, controlId, outputstring, statusresult);
                    roOutputPrivateFun.RebackExtentReport(Status.Info, controlName, controlId, outstring);
                    outstring = outstring.Replace("<br/>", "");
                    Console.WriteLine(outstring);
                    break;


                case RoLogStatus.Debug:
                    statusresult = "执行Debug信息";
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    outstring = roOutputPrivateFun.RebackOutputConsole(controlName, controlId, outputstring, statusresult);
                    roOutputPrivateFun.RebackExtentReport(Status.Debug, controlName, controlId, outstring);
                    outstring = outstring.Replace("<br/>", "");
                    Console.WriteLine(outstring);
                    break;
            }
        }
    }


    /// <summary>
    /// 输出控制台的私有方法
    /// </summary>
    class RoOutputPrivateFun
    {
        /// <summary>
        /// 控制台输出语句
        /// </summary>
        /// <param name="outputstring">输出语句</param>
        /// <param name="statusresult">执行状态</param>
        /// <returns></returns>
        public string RebackOutputConsole(string outputstring, string statusresult)
        {
            //获取当前时间
            string nowtime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            StringBuilder outStringBuilder = new StringBuilder();
            outStringBuilder.AppendLine("当前时间: " + nowtime + "<br/>");
            outStringBuilder.AppendLine("执行状态: " + statusresult + "<br/>");
            outStringBuilder.AppendLine("追加信息: " + outputstring + "<br/>");
            return outStringBuilder.ToString();
        }


        /// <summary>
        /// 控制台输出语句
        /// </summary>
        /// <param name="controlName">操作名称</param>
        /// <param name="controlId">控件名称</param>
        /// <param name="outputstring">输出语句</param>
        /// <param name="statusresult">执行状态</param>
        /// <returns></returns>
        public string RebackOutputConsole(string controlName, string controlId, string outputstring, string statusresult)
        {
            //获取当前时间
            string nowtime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            StringBuilder outStringBuilder = new StringBuilder();
            outStringBuilder.AppendLine("当前时间: " + nowtime + "<br/>");
            outStringBuilder.AppendLine("执行状态: " + statusresult + "<br/>");
            outStringBuilder.AppendLine("操作类型: " + controlName + "<br/>");
            outStringBuilder.AppendLine("控件名称: " + controlId + "<br/>");
            outStringBuilder.AppendLine("追加信息: " + outputstring + "<br/>");
            return outStringBuilder.ToString();
        }

        /*
         * =========================================================================================================================
                                                       返回extentreport带有附加信息          
         * =========================================================================================================================
         */

        /// <summary>
        /// 返回extentreport
        /// 带有附加信息
        /// </summary>
        /// <param name="roLogStatus">状态</param>
        /// <param name="controlname">操作名称</param>
        /// <param name="controlid">控件ID</param>
        /// <param name="outputstring">追加信息</param>
        public void RebackExtentReport(Status roLogStatus, string controlname, string controlid, string outputstring)
        {
            //初始化
            IRoExtentReport roExtentReport = new RoExtentReport();

            string stepname = "操作名称:<br/>" + controlname;
            StringBuilder exdetails = new StringBuilder();

            exdetails.AppendLine("操作的控件ID:" + controlid + "<br/>");
            exdetails.AppendLine("额外追加信息:<br/>" + outputstring + "<br/>");
            roExtentReport.AddTestsChildNode(roLogStatus, stepname, exdetails.ToString());
        }
    }
}
